package slotMachineGUI;


import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import org.json.JSONArray;
import org.json.JSONObject;

@SuppressWarnings("serial")
public class GMap extends JPanel {
    
    //flag
    public static final int SOURCE_FLAG = 0;
    public static final int DESTINATION_FLAG = 1;
    
	//Max size
	private static final int WIDTH = 640;
	private static final int HEIGHT = 640;
	
	//url 參數
	private static final String SOURCE_ADDRESS_URL = "&markers=label:S|";
	private static final String DESTINATION_ADDRESS_URL = "&markers=label:D|";
	private static final String STATIC_MAP_URL = 
			"http://maps.google.com/maps/api/staticmap?size=" 
			+ WIDTH + "x" + HEIGHT + "&sensor=false";
	private static final String GECODING_URL =
	        "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&address=";
	
	private BufferedImage map;
	
	//空白建構函式
	public GMap() {
		try {
			map = ImageIO.read(new URL(STATIC_MAP_URL 
					+ "&center=成功大學"));
			setPreferredSize(new Dimension(WIDTH, HEIGHT));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//只產生目的位置的建構函式
	public GMap(String targetAddress) {
		try {
			map = ImageIO.read(new URL(STATIC_MAP_URL 
					+ "&center=" + targetAddress));
			setPreferredSize(new Dimension(WIDTH, HEIGHT));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//有起始點跟目的位置的建構函式
	public GMap(String sourceAddress, String destinationAddress) {
		try {
			map = ImageIO.read(new URL(STATIC_MAP_URL 
					+ toMarkerURL(sourceAddress, SOURCE_FLAG) 
					+ toMarkerURL(destinationAddress, DESTINATION_FLAG)));
			setPreferredSize(new Dimension(WIDTH, HEIGHT));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//只有目的位置，以該位置設為中心產生地圖
	public void setAddress(String targetAddress) {
		try {
			map = ImageIO.read(new URL(STATIC_MAP_URL 
					+ "&center=" + targetAddress 
					+ toMarkerURL(targetAddress, DESTINATION_FLAG)));
			invalidate();
			repaint();
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//具有原點與目的位置，產生具有路徑規劃的地圖
	public void setAddress(String sourceAddress, String destinationAddress) {
		try {
			
			JsonData Json = new JsonData(sourceAddress, destinationAddress);
			map = ImageIO.read(new URL(STATIC_MAP_URL 
                    + toMarkerURL(sourceAddress, SOURCE_FLAG) 
                    + toMarkerURL(destinationAddress, DESTINATION_FLAG) 
					+ "&path=color:0x0000ff|weight:5|" + Json.getRouteString() ));
			invalidate();
			repaint();
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	//將地址轉成經緯度，並符合url的格式，flag判斷是起始點或目的位置
	public static String toMarkerURL(String address, int flag) {
	    
	    String markerURL = null;        
        String jsonOutput;
        JSONObject jsonObject;
        JSONObject result;
        JSONObject jsonLatLng;
        String lat = null, lng = null;
        
        try {
            //用json去解析地址轉經緯度的結果
            jsonOutput = JsonData.sentHttpRequest(GECODING_URL + address);
            jsonObject = new JSONObject(jsonOutput);
            result = jsonObject.getJSONArray("results").getJSONObject(0);            
            jsonLatLng = result.getJSONObject("geometry").getJSONObject("location");            
            lat = Double.toString(jsonLatLng.getDouble("lat"));
            lng = Double.toString(jsonLatLng.getDouble("lng"));            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        switch(flag) {   //return經緯度的結果
        case SOURCE_FLAG:
            markerURL = SOURCE_ADDRESS_URL + lat + "," + lng;
            break;
        case DESTINATION_FLAG:
            markerURL = DESTINATION_ADDRESS_URL + lat + "," + lng;
            break;
        }
        return markerURL;
    }
	
	@Override
	public void paintComponent(Graphics g) {
		g.drawImage(map, 0, 0, null); //draw map
	}
}
